package com.geli.smarttools.tool;

import android.hardware.ConsumerIrManager;
import android.util.Log;

import com.geli.smarttools.bean.SettingBean;
import com.geli.smarttools.date.CodeCommand;

public class PackTool {
    private ConsumerIrManager consumerIrManager ;
    public PackTool( ConsumerIrManager consumerIrManager){
        this.consumerIrManager =consumerIrManager ;
    }

    public void sendCmdPack(SettingBean settingBean_Event) {
        int mPower = settingBean_Event.getmPower();
        int mTmp = settingBean_Event.getmTmp();
        int mMode = settingBean_Event.getmMode();
        int menergy = settingBean_Event.getmenergy();
        int mWindDir = settingBean_Event.getmWindDir();
        int mWindCount = settingBean_Event.getmWindCount();

        int tmWindDir = 0;//二进制方向
        if (mWindDir == 2) {
            tmWindDir = 1;
        }

        else if (mWindDir == 1) {
            tmWindDir = 0;
        }

        else {
            tmWindDir = 0;
        }
        //根据所选模式确定检验码
        //校验码 = [(模式 – 1) + (温度 – 16) + 5 +左右扫风+换气+节能]取二进制后四位，再逆序
        int check = (mMode - 1) + (mTmp - 16) + 5 + tmWindDir + menergy + menergy;//十进制数字
        String two_chack = Integer.toBinaryString(check);//转换成二进制
        //如果大于四位进行裁剪
        //补零
        switch (two_chack.length()){
            case 3:
                two_chack = "0"+two_chack;
                break;
            case 2:
                two_chack = "00"+two_chack;
                break;
            case 1:
                two_chack = "000"+two_chack;
                break;
        }
        two_chack = two_chack.substring(two_chack.length() - 4, two_chack.length());//取后四位
        String Cut = new StringBuffer(two_chack).reverse().toString();//倒序


        //分解字符（承载最后四个逆序字符）
        char[] item = new char[5];
        for (int i = 0; i < Cut.length(); i++) {
            item[i] = Cut.charAt(i);
        }
        int[] base = CodeCommand.base;

        //第一步 替换校验码  （分七步）
        //取出数组里的四个数
        int one = Integer.valueOf(String.valueOf(item[0]));
        int two = Integer.valueOf(String.valueOf(item[1])) ;
        int three = Integer.valueOf(String.valueOf(item[2])) ;
        int four = Integer.valueOf(String.valueOf(item[3]));
        //64-67位为校验码 131、132 \ 133、134 \ 135、136 \ 137、138
        //第一个数
        if (one == 1) {
            //将大数组里的130、131位置1
            base[130] = CodeCommand.check_d;
            base[131] = CodeCommand.check_u;

        }
        //第二个数
        if (two == 1) {
            base[132] = CodeCommand.check_d;
            base[133] = CodeCommand.check_u;

        } else {
            Log.e("gaoyu", "第二个数是0");
            //将大数组里的132、133位不用变
        }
        //第三个数
        if (three == 1) {
            base[134] = CodeCommand.check_d;
            base[135] = CodeCommand.check_u;

        }
        //第四个数
        if (four == 1) {
            base[136] = CodeCommand.check_d;
            base[137] = CodeCommand.check_u;
        }

        //第二步 开关  8/9
        if (mPower == 1) {
            base[8] = CodeCommand.onedown;
            base[9] = CodeCommand.oneup;
        } else {
            base[8] = CodeCommand.zerodown;
            base[9] = CodeCommand.zeroup;
        }

        //第三步 温度 16-30度   数组中18、25
        switch (mTmp) {
            case 16:
                //默认十六
                break;
            case 17:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 18:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 19:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 20:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 21:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 22:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 23:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.zerodown;
                base[25] = CodeCommand.zeroup;
                break;
            case 24:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            case 25:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            case 26:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            case 27:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.zerodown;
                base[23] = CodeCommand.zeroup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            case 28:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            case 29:
                base[18] = CodeCommand.onedown;
                base[19] = CodeCommand.oneup;
                base[20] = CodeCommand.zerodown;
                base[21] = CodeCommand.zeroup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            case 30:
                base[18] = CodeCommand.zerodown;
                base[19] = CodeCommand.zeroup;
                base[20] = CodeCommand.onedown;
                base[21] = CodeCommand.oneup;
                base[22] = CodeCommand.onedown;
                base[23] = CodeCommand.oneup;
                base[24] = CodeCommand.onedown;
                base[25] = CodeCommand.oneup;
                break;
            default:
                break;
        }

        //第四步  模式  2-7
        switch (mMode) {
            case 0:
                base[2] = CodeCommand.zerodown;
                base[3] = CodeCommand.zeroup;
                base[4] = CodeCommand.zerodown;
                base[5] = CodeCommand.zeroup;
                base[6] = CodeCommand.zerodown;
                base[7] = CodeCommand.zeroup;
                break;
            case 1:
                base[2] = CodeCommand.onedown;
                base[3] = CodeCommand.oneup;
                base[4] = CodeCommand.zerodown;
                base[5] = CodeCommand.zeroup;
                base[6] = CodeCommand.zerodown;
                base[7] = CodeCommand.zeroup;
                break;
            case 2:
                base[2] = CodeCommand.zerodown;
                base[3] = CodeCommand.zeroup;
                base[4] = CodeCommand.onedown;
                base[5] = CodeCommand.oneup;
                base[6] = CodeCommand.zerodown;
                base[7] = CodeCommand.zeroup;
                break;
            case 3:
                base[2] = CodeCommand.onedown;
                base[3] = CodeCommand.oneup;
                base[4] = CodeCommand.onedown;
                base[5] = CodeCommand.oneup;
                base[6] = CodeCommand.zerodown;
                base[7] = CodeCommand.zeroup;
                break;
            case 4:
                base[2] = CodeCommand.zerodown;
                base[3] = CodeCommand.zeroup;
                base[4] = CodeCommand.zerodown;
                base[5] = CodeCommand.zeroup;
                base[6] = CodeCommand.onedown;
                base[7] = CodeCommand.oneup;
                break;
        }
        //第五步 节电、换气 48-51
        if (menergy == 1) {
            Log.e("gaoyu", "开启节电换气");
            base[48] = CodeCommand.onedown;
            base[49] = CodeCommand.oneup;
            base[50] = CodeCommand.onedown;
            base[51] = CodeCommand.oneup;
        }else{
            base[48] = CodeCommand.zerodown;
            base[49] = CodeCommand.zeroup;
            base[50] = CodeCommand.zerodown;
            base[51] = CodeCommand.zeroup;
        }
        //第六步  风向  1、上下 36 数组 74.75   2、左右 40  80.81
        switch (mWindDir) {
            case 0:
                //默认
                break;
            case 1:
                base[74] = CodeCommand.onedown;
                base[75] = CodeCommand.oneup;
                break;
            case 2:
                base[80] = CodeCommand.onedown;
                base[81] = CodeCommand.oneup;
                break;
        }

        //第七步  风量  10-13
        switch (mWindCount) {
            case 0:
                //默认
                break;
            case 1:
                base[10] = CodeCommand.onedown;
                base[11] = CodeCommand.oneup;
                base[12] = CodeCommand.zerodown;
                base[13] = CodeCommand.zeroup;
                break;
            case 2:
                base[10] = CodeCommand.zerodown;
                base[11] = CodeCommand.zeroup;
                base[12] = CodeCommand.onedown;
                base[13] = CodeCommand.oneup;
                break;
            case 3:
                base[10] = CodeCommand.onedown;
                base[11] = CodeCommand.oneup;
                base[12] = CodeCommand.onedown;
                base[13] = CodeCommand.oneup;
                break;
        }

        //发送完数据将大数组还原
        sendIrMsg( base);
        base = CodeCommand.base;
    }

    /**
     * @param pattern 指以微秒为单位的红外开和关的交替时间
     */
    private void sendIrMsg( int[] pattern) {
        if(consumerIrManager==null){
            return;
        }
        // 红外传输的频率，一般的遥控板都是38KHz
        consumerIrManager.transmit(38000, pattern);
    }

}
